package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.UserLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@Tag(name = "用户声音播放进度管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserListenProcessApiController {

	@Autowired
	private UserListenProcessService userListenProcessService;

	/**
	 * 获取声音播放进度
	 *
	 * @param trackId
	 * @return
	 */
	@UserLogin(required = false)
	@Operation(summary = "获取声音播放进度")
	@GetMapping("/userListenProcess/getTrackBreakSecond/{trackId}")
	public Result<BigDecimal> getTrackBreakSecond(@PathVariable("trackId") Long trackId){
		//获取当前用户id
		Long userId = AuthContextHolder.getUserId();
		//如果用户登录则获取声音播放进度,未登录不获取
		if(userId != null){
			BigDecimal bigDecimal = userListenProcessService.getTrackBreakSecond(userId,trackId);
			return Result.ok(bigDecimal);
		}
		return Result.ok(BigDecimal.valueOf(0));
	}


	/**
	 * 更新声音播放进度
	 * @param userListenProcessVo
	 * @return
	 */
	@UserLogin(required = false)
	@Operation(summary = "更新声音播放进度")
	@PostMapping("/userListenProcess/updateListenProcess")
	public Result updateListenProcess(@RequestBody UserListenProcessVo userListenProcessVo){
		//查询用户是否登录,已登录再去更新之前的播放进度
		Long userId = AuthContextHolder.getUserId();
		if(userId != null){
			userListenProcessService.updateListenProcess(userId,userListenProcessVo);
		}
		return Result.ok();
	}

	/**
	 * 获取当前用户最近播放声音
	 * @return {albumId:1,trackId:12}
	 */
	@UserLogin
	@Operation(summary = "获取用户最近一次播放记录")
	@GetMapping("/userListenProcess/getLatelyTrack")
	public Result<Map<String,Long>> getLatelyTrack(){
		Long userId = AuthContextHolder.getUserId();
		Map<String,Long> map = userListenProcessService.getLatelyTrack(userId);

		return Result.ok(map);
	}



}

